import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/pages/main/dao/dao_list_controller.dart';

import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/empty_widget.dart';
import 'package:yysk_app/widget/text_linear_gradient_widget.dart';

class DaoHeaderDelegate extends SliverPersistentHeaderDelegate {
  @override
  double get minExtent => 90.px;

  @override
  double get maxExtent => 90.px;

  @override
  Widget build(
    BuildContext context,
    double shrinkOffset,
    bool overlapsContent,
  ) {
    double top = MediaQuery.of(context).padding.top;

    return Container(
      color: AppColors.backgroundColor,
      child: Stack(
        children: [
          Positioned(
            left: 0,
            bottom: 0,
            top: 0,
            right: 0,
            child: Center(
              child: Opacity(
                opacity: (1),
                child: Container(
                  width: 375.px,
                  height: maxExtent,
                  constraints: BoxConstraints(
                    maxHeight: maxExtent,
                    minHeight: minExtent,
                  ),
                  decoration: BoxDecoration(
                    // color: AppColors.backgroundColorBlack,
                    color: Colors.transparent,
                  ),
                  child: Stack(
                    children: [
                      Center(
                        child: Column(
                          children: [
                            SizedBox(height: 40.px),
                            ImageAssetWidget(
                              imageUrl: 'static/images/dao_icon.png',
                              width: 120.px,
                              height: 44.px,
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}

class DaoPage extends StatefulWidget {
  static String path = '/DaoPage';
  DaoPage({super.key});

  @override
  State<StatefulWidget> createState() => _DaoPageStates();
}

class _DaoPageStates extends State<DaoPage> {
  final ScrollController scrollController = ScrollController();

  final DaoListController controller = Get.put(DaoListController());
  @override
  void initState() {
    super.initState();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        controller.getDaoPageListData(loadMore: true);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),
          Obx(
            () => Container(
              padding: EdgeInsets.zero,
              // color: AppColors.backgroundColorBlack,
              child: RefreshIndicator(
                // backgroundColor: Colors.black,
                backgroundColor: Colors.transparent,

                // ignore: sort_child_properties_last
                child: CustomScrollView(
                  controller: scrollController,
                  physics: AlwaysScrollableScrollPhysics(),
                  slivers: [
                    SliverToBoxAdapter(
                      child: Container(
                        width: 375.px,
                        padding: EdgeInsets.only(top: top),
                        decoration: BoxDecoration(
                          // color: AppColors.backgroundColorBlack,
                        ),
                        child: Stack(
                          children: [
                            Center(
                              child: Column(
                                children: [
                                  ImageAssetWidget(
                                    imageUrl: 'static/images/dao_icon.png',
                                    width: 120.px,
                                    height: 44.px,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    if (controller.listDao.isEmpty)
                      const SliverToBoxAdapter(child: EmptyWidget()),
                    // SliverList(
                    //   delegate: SliverChildBuilderDelegate(
                    //     childCount: controller.listDao.length,
                    //     (BuildContext context, int index) {
                    //       return _buildItem(context, index);
                    //     },
                    //   ),
                    // ),
                    SliverPadding(
                      padding: EdgeInsets.fromLTRB(12.px, 0.px, 12.px, 0.px),
                      sliver: SliverList(
                        delegate: SliverChildBuilderDelegate(
                          childCount: controller.listDao.length,
                          (BuildContext context, int index) {
                            return _buildItem(context, index);
                          },
                        ),
                      ),
                    ),
                  ],
                ),
                onRefresh: _handleRefresh,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future<void> _handleRefresh() async {
    controller.getDaoPageListData();
    return;
  }

  Widget _buildItem(BuildContext context, int index) {
    final item = controller.listDao[index];
    return GestureDetector(
      onTap: () {},
      child: Container(
        // height: 108.px,
        width: 351.px,
        margin: EdgeInsets.fromLTRB(0, 8.px, 0, 0),
        padding: EdgeInsets.fromLTRB(14.px, 14.px, 14.px, 14.px),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8.px),
          border: Border.all(
            color: AppColors.backgroundColorGrey2,
            width: 1.px,
          ),
          color: AppColors.backgroundColorGrey2,
        ),
        child: Stack(
          children: [
            Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,

                      children: [
                        Row(
                          children: [
                            SizedBox(
                              width: 32.px,
                              height: 32.px,
                              child: Center(
                                child: Container(
                                  clipBehavior: Clip.antiAlias,

                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(100.px),
                                    // color: AppColors.fontColorRed,
                                  ),
                                  child: LazyImage(
                                    imageUrl: '${item.headImageUrl}',
                                    width: 32.px,
                                    height: 32.px,
                                    borderRadius: 100,
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(width: 12.px),
                            SizedBox(
                              width: 160.px,
                              child: TextLinearGradientWidget(
                                txt: '${item.daoName}',
                                fontSize: 14.px,
                                textAlign: TextAlign.left,
                                startColor: AppColors.fontColor,
                                endColor: AppColors.fontColor,
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: 8),
                        SizedBox(
                          width: 300.px,
                          child: Text(
                            '${item.daoIntroduce}',
                            style: TextStyle(
                              color: AppColors.backgroundColorGrey,
                              fontSize: 13.px,
                              overflow: TextOverflow.ellipsis,
                            ),
                            textAlign: TextAlign.start,
                            maxLines: 2,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
